<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

/**
 * 首页
 */
class Home extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function get_qun()
    {
        $group_id = "162965686910978";
        require_once "./huanxin/Easemob.class.php";
        $e = new \Easemob();

        $result_u = $e->getGroupUsers($group_id);
        var_dump($result_u);
        die;
    }

    /*
     * 首页金刚区
     */
    public function home_jingang()
    {
        $request = [
            'jingang_id' => $this->request->param('jingang_id'),//金刚id
            'page' => $this->request->param('page'),
            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('jingang_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $jingang_id = $request['jingang_id'];
        $uid = $request['uid'];
        $goods_ids = "";
        if ($uid) {
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
//            if ($res == 0) {
//                $this->error('账号异常','',206);
//            }
//            if ($res == 1) {
//                $this->error('非法的请求','',201);
//            }
            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
            $goods_id = array();

            if ($pid) {  //我的隐藏数据
                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
                foreach ($useryincang as $v) {
                    $goods_id[] = $v['goods_id'];
                }
            }
            if ($goods_id) {
                $goods_ids = implode(',', $goods_id);
            }

        }

        $page = $request['page'];
        if (empty($page)) {
            $page = 1;
        }
        $pagesize = 10;
        if ($goods_ids) {
            $jingang_goods = Db::name("jingang_newgoods a")
                ->join('goods b', 'a.goods_id = b.id')
                ->where(" a.goods_id not in ($goods_ids)")
                ->where("a.jingang_id = {$jingang_id} and b.status = 1")
                ->field("a.jingang_id,a.goods_id,b.name goods_name,b.price,b.old_price,b.image goods_image,b.sale_num,b.integral,b.province,b.city,vip_price")
                ->page($page, $pagesize)
                ->select();

        } else {
            $jingang_goods = Db::name("jingang_newgoods a")
                ->join('goods b', 'a.goods_id = b.id')
                ->where("a.jingang_id = {$jingang_id} and b.status = 1")
                ->field("a.jingang_id,a.goods_id,b.name goods_name,b.price,b.old_price,b.image goods_image,b.sale_num,b.integral,b.province,b.city,vip_price")
                ->page($page, $pagesize)
                ->select();
        }
        foreach ($jingang_goods as $k => $v) {
            $jingang_goods[$k]['goods_image'] = path($v['goods_image']);
        }
        $this->success('成功', $jingang_goods, 200);
    }


    public function speciality()
    {
        $province_id = $this->request->param('province_id');//
        $city_id = $this->request->param('city_id');//
        $pagesize = $this->request->param('pagesize') ?: 10;
        $goods_id = $this->request->param('goods_id');
        $notGoodIdList = [];
        if (isset($goods_id) && !empty($goods_id)) {
            $notGoodIdList = explode(',', $goods_id);
        }
        if(!isset($province_id) && !isset($city_id)){
            $list = Db::name("goods")
                ->where("status = 1")
                ->where("is_speciality = 1")
                ->where(function ($query) use ($province_id, $city_id) {
                    if (isset($province_id) && !empty($province_id)) {
                        $query->where("province_id", $province_id);
                    }
                    if (isset($city_id) && !empty($city_id)) {
                        $query->where("city_id", $city_id);
                    }
                })
                ->whereNotIn("id", $notGoodIdList)
                ->where("price", "<=",20)
                ->orderRaw('rand()')
                ->field("id,name,image,price,old_price,vip_price,sale_num,integral,province,city,vip_price")
                ->limit($pagesize)
                ->select();
        }else{
            $list = Db::name("goods")
                ->where("status = 1")
                ->where("is_speciality = 1")
                ->where(function ($query) use ($province_id, $city_id) {
                    if (isset($province_id) && !empty($province_id)) {
                        $query->where("province_id", $province_id);
                    }
                    if (isset($city_id) && !empty($city_id)) {
                        $query->where("city_id", $city_id);
                    }
                })
                ->whereNotIn("id", $notGoodIdList)
                ->orderRaw('rand()')
                ->field("id,name,image,price,old_price,vip_price,sale_num,integral,province,city,vip_price")
                ->limit($pagesize)
                ->select();
        }
        $goodsList = $list;
        $num = $pagesize - count($goodsList);
        if ($num) {
            foreach ($goodsList as $value) {
                $notGoodIdList[] = $value['id'];
            }
            $jixu = 0;
            if (isset($city_id) && !empty($city_id)) {
                $jixu = 1;
            }
            $list = Db::name("goods")
                ->where("status = 1")
                ->where("is_speciality = 1")
                ->where(function ($query) use ($province_id, $city_id) {
                    if (isset($city_id) && !empty($city_id)) {
                        $city = DB::name('district')->where('level', 2)->where('district_id', $city_id)->find();
                        if ($city) {
                            $query->where("province_id", $city['pid']);
                        }
                    }
                })
                ->whereNotIn("id", $notGoodIdList)
                ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
                ->limit($num)
                ->select();
            foreach ($list as $value) {
                $notGoodIdList[] = $value['id'];
                $goodsList[] = $value;
            }
            $num = $pagesize - count($goodsList);
            if ($num) {
                if ($jixu) {
                    $list = Db::name("goods")
                        ->where("status = 1")
                        ->where("is_speciality = 1")
                        ->whereNotIn("id", $notGoodIdList)
                        ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                        ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
                        ->limit($num)
                        ->select();
                    foreach ($list as $value) {
                        $notGoodIdList[] = $value['id'];
                        $goodsList[] = $value;
                    }
                }
                $num = $pagesize - count($goodsList);
                if ($num) {
                    $list = Db::name("goods")
                        ->where("status = 1")
                        ->where("is_speciality = 0")
                        ->whereNotIn("id", $notGoodIdList)
                        ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                        ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
                        ->limit($num)
                        ->select();
                    foreach ($list as $value) {
                        $notGoodIdList[] = $value['id'];
                        $goodsList[] = $value;
                    }
                }
            }
        }
        foreach ($goodsList as $k => $v) {
            $goodsList[$k]['goods_image'] = path($v['image']);
            preg_match_all("/./us", $v['name'], $match);
            $goodsList[$k]['length'] = count($match[0]);
            if ($goodsList[$k]['length'] > 10) {
                $goodsList[$k]['height'] = 536;
            } else {
                $goodsList[$k]['height'] = 500;
            }
        }
        $this->success('成功', $goodsList, 200);
    }

    public function car_tuijian()
    {
        $uid = $this->request->param("uid") ?: 0;
//        $token = $this->request->param("token") ?: 0;//
//        $jm_str = $this->request->param("jm_str") ?: 0;//
//        $res = yanzheng($uid, $token, $jm_str);
//        if ($res == 0) {
//            $this->error('账号异常', '', 206);
//        }
//        if ($res == 1) {
//            $this->error('非法的请求', '', 201);
//        }
//        if (empty($uid)) {
//            $this->error("请登录", '', '201');
//        }
        $pagesize = $this->request->param('pagesize') ?: 10;
        $page = $this->request->param('page') ?: 1;
        $goods_id = $this->request->param('goods_id');
        $notGoodIdList = [];
        if (isset($goods_id) && !empty($goods_id)) {
            $notGoodIdList = explode(',', $goods_id);
        }
        $carNum = Db::name("buy_car")->where('userhome_id', $uid)->count();
        $goodsList = [];
        if ($carNum) {
            $num = $page / $carNum;
            if (!is_integer($num)) {
                $num = intval($num) + 1;
            }
            $num = (($carNum * $num) - $page);
            $num = $carNum - $num;
            $car = Db::name("buy_car t1")
                ->join("goods t2", 't1.goods_id = t2.id')
                ->where('t1.userhome_id', $uid)
                ->order('t1.id', 'desc')
                ->field('t2.goods_one_type_id,t2.goods_two_type_id,t2.goods_three_type_id,t2.vip_price')
                ->limit($num - 1, 1)
                ->select();
            if (isset($car[0])) {
                $car = $car[0];
                $list = Db::name("goods")
                    ->where("status = 1")
                    ->where(function ($query) use ($car) {
//                        if (isset($car['goods_one_type_id']) && !empty($car['goods_one_type_id'])) {
//                            $query->whereOr("t1.goods_one_type_id", $car['goods_one_type_id']);
//                        }
                        if (isset($car['goods_two_type_id']) && !empty($car['goods_two_type_id'])) {
                            $query->whereOr("goods_two_type_id", $car['goods_two_type_id']);
                        }
                        if (isset($car['goods_three_type_id']) && !empty($car['goods_three_type_id'])) {
                            $query->whereOr("goods_three_type_id", $car['goods_three_type_id']);
                        }
                    })
                    ->whereNotIn("id", $notGoodIdList)
                    ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                    ->field("id,name,image,price,old_price,sale_num,integral,province,city,goods_one_type_id,goods_two_type_id,goods_three_type_id,vip_price")
                    ->limit($pagesize)
                    ->select();
                foreach ($list as $value) {
                    $notGoodIdList[] = $value['id'];
                }
                $goodsList = $list;
                $num = $pagesize - count($goodsList);
            } else {
                $num = $pagesize;
            }
        } else {
            $num = $pagesize;
        }
        if ($num) {
            $list = Db::name("goods")
                ->where("status = 1")
                ->whereNotIn("id", $notGoodIdList)
                ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                ->field("id,name,image,price,old_price,sale_num,integral,province,city,goods_one_type_id,goods_two_type_id,goods_three_type_id,vip_price")
                ->limit($num)
                ->select();
            foreach ($list as $value) {
                $notGoodIdList[] = $value['id'];
                $goodsList[] = $value;
            }
        }
        foreach ($goodsList as $k => $v) {
            $goodsList[$k]['goods_image'] = path($v['image']);
            preg_match_all("/./us", $v['name'], $match);
            $goodsList[$k]['length'] = count($match[0]);
            if ($goodsList[$k]['length'] > 10) {
                $goodsList[$k]['height'] = 536;
            } else {
                $goodsList[$k]['height'] = 500;
            }
        }
        $this->success('成功', $goodsList, 200);
    }

    public function handpick()
    {
        $pagesize = $this->request->param('pagesize') ?: 10;
        $goods_id = $this->request->param('goods_id');
        $notGoodIdList = [];
        if (isset($goods_id) && !empty($goods_id)) {
            $notGoodIdList = explode(',', $goods_id);
        }
        $list = Db::name("goods")
            ->where("status = 1")
            ->where("is_handpick = 1")
            ->whereNotIn("id", $notGoodIdList)
            ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
            ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
            ->limit($pagesize)
            ->select();
        $goodsList = $list;
        foreach ($goodsList as $k => $v) {
            $goodsList[$k]['goods_image'] = path($v['image']);
            preg_match_all("/./us", $v['name'], $match);
            $goodsList[$k]['length'] = count($match[0]);
            if ($goodsList[$k]['length'] > 10) {
                $goodsList[$k]['height'] = 536;
            } else {
                $goodsList[$k]['height'] = 500;
            }
        }
        $this->success('成功', $goodsList, 200);
    }

    /*
     *为您推荐 7 天以内的搜索  没有 就从商品表随机抽取
     */
    public function home_tuijian()
    {
        $district_id = $this->request->param('district_id');
        $request = [
            'uid' => $this->request->param('uid'),
            'page' => $this->request->param('page') ?: 1,

        ];
        $page = $request['page'];
        $uid = $request['uid'];
        $userTuij = 0;
        if ($uid) {
            $userTuij = 1;
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
            if ($res == 0) {
                $userTuij = 0;
            }
            if ($res == 1) {
                $userTuij = 0;
            }
        }
        $pageSize = 10;
        $searchNum = intval(($pageSize / 100) * 50);
        $logNum = intval(($pageSize / 100) * 30);
        $hostNum = intval(($pageSize / 100) * 20);
        $goodsList = [];
        $notGoodIdList = [];
        if ($userTuij) {
            $userLink = Db::name("user_link")->where("userId = $uid")->find();
            if (!$userLink) {
                $userLink = [];
                $userLink['userId'] = $uid;
                Db::name("user_link")->insert($userLink);
            } else {
                if ($page == 1) {
                    Db::name("user_link")->where("userId = $uid")->update(['goodIdList' => null]);
                }
            }
            $userLink = Db::name("user_link")->where("userId = $uid")->find();
            if (isset($userLink['goodIdList']) && !empty($userLink['goodIdList'])) {
                $notGoodIdList = explode(';', $userLink['goodIdList']);
            }
            $stime = strtotime('-7 days');
            $search = Db::name("search")->where("uid = {$uid} and createtime >= {$stime}")->field("name,createtime")->select();
            $names = array();
            foreach ($search as $v) {
                $names[] = $v['name'];
            }
            if (isset($names[0])) {
                $list = Db::name("goods")
                    ->where("status = 1")
                    ->where(function ($query) use ($names) {
                        $query->where("name like '%" . $names[0] . "%'");
                        foreach ($names as $key => $value) {
                            if ($key != 0) {
                                $query->whereOr("name like '%" . $value . "%'");
                            }
                        }
                    })
                    ->whereNotIn("id", $notGoodIdList)
                    ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                    ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
                    ->limit($searchNum)
                    ->select();
                $goodsList = $list;
                $logNum = $logNum + ($searchNum - count($goodsList));
            } else {
                $logNum = $logNum + $searchNum;
            }
            foreach ($goodsList as $value) {
                $notGoodIdList[] = $value['id'];
            }
            if (isset($userLink['goodsOneTypeIdList']) && !empty($userLink['goodsOneTypeIdList'])) {
                $goodsOneTypeIdList = explode(';', $userLink['goodsOneTypeIdList']);
            }
            $goods = [];
            if (isset($goodsOneTypeIdList[0])) {
                $list = Db::name("goods")
                    ->where("status = 1")
                    ->whereIn("goods_one_type_id", $goodsOneTypeIdList)
                    ->whereNotIn("id", $notGoodIdList)
                    ->orderRaw('rand()')
//                    ->order('sale_num', 'desc')
//                    ->order('createtime', 'desc')
                    ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
                    ->limit($logNum)
                    ->select();
                $goods = $list;
                $hostNum = $hostNum + ($logNum - count($goods));
            } else {
                $hostNum = $hostNum + $logNum;
            }
            foreach ($goods as $value) {
                $notGoodIdList[] = $value['id'];
                $goodsList[] = $value;
            }
        } else {
            $hostNum = $searchNum + $logNum + $hostNum;
        }
        $list = Db::name("goods")
            ->where("status = 1")
            ->whereNotIn("id", $notGoodIdList)
            ->orderRaw('rand()')
//            ->order('sale_num', 'desc')
//            ->order('createtime', 'desc')
            ->field("id,name,image,price,old_price,sale_num,integral,province,city,vip_price")
            ->limit($hostNum)
            ->select();
        $goods = $list;
        foreach ($goods as $value) {
            $notGoodIdList[] = $value['id'];
            $goodsList[] = $value;
        }
        if ($userTuij) {
            $notGoodIdList = implode(';', $notGoodIdList);
            Db::name("user_link")->where("userId = $uid")->update(['goodIdList' => $notGoodIdList]);
        }
        foreach ($goodsList as $k => $v) {
            $goodsList[$k]['goods_image'] = path($v['image']);
            preg_match_all("/./us", $v['name'], $match);
            $goodsList[$k]['length'] = count($match[0]);
            if ($goodsList[$k]['length'] > 10) {
                $goodsList[$k]['height'] = 536;
            } else {
                $goodsList[$k]['height'] = 500;
            }
        }
        $this->success('成功', $goodsList, 200);
    }

    /*
     * 新人福利
     */
    public function newcomer_welfare()
    {
        $page = $this->request->param('page');
        if (empty($page)) {
            $page = 1;
        }
        //新人福利
        $newcomer_welfare = Db::name("newcomer_welfare a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.price,b.name,b.introduce,b.old_price,b.image,b.sale_num,b.integral,b.province,b.city,vip_price")
            ->order('weigh desc')
            ->limit(($page - 1) * 10, 10)
            ->select();
        foreach ($newcomer_welfare as $k => $v) {
            $newcomer_welfare[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $newcomer_welfare, 200);
    }

    /*
    * 限时秒杀
    */
    public function seckill()
    {
        $page = $this->request->param('page');
        if (empty($page)) {
            $page = 1;
        }
        $is_show = Db::name("is_show")->where('name',"限时秒杀")->find();
        $seckill = Db::name("seckill a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.all_number,a.number,a.price,b.name,b.introduce,b.old_price,a.image,b.sale_num,b.integral,b.province,b.city,a.stime,a.etime,vip_price")
            ->order('weigh desc')
            ->limit(($page - 1) * 10, 10)
            ->select();
        foreach ($seckill as $k => $v) {
            $seckill[$k]['image'] = path($v['image']);
            if(!empty($is_show)){
                $seckill[$k]['etime'] = $is_show['etime'] - time();
            }
        }
        $this->success('成功', $seckill, 200);
    }

    /*
     * 超值折扣
     */
    public function discount()
    {
        $page = $this->request->param('page');
        if (empty($page)) {
            $page = 1;
        }
        //超值折扣
        $discount = Db::name("discount a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.price,b.old_price,b.name,b.introduce,a.image,b.sale_num,b.integral,b.province,b.city,vip_price")
            ->order('weigh desc')
            ->limit(($page - 1) * 10, 10)
            ->select();
        foreach ($discount as $k => $v) {
            $discount[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $discount, 200);
    }


    /*
     *6、首页
     */
    public function home()
    {
        $request = [
            'uid' => $this->request->param('uid'),
//            'page' => $this->request->param('page'),
        ];

        $uid = $request['uid'];
//        $page = $request['page'];
//        if(!$page){
//            $page = 1;
//        }


        $data = array();

        //店名
        if ($uid) {
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
            if ($res == 0) {
                $this->error('账号异常', '', 206);
            }
            if ($res == 1) {
                $this->error('非法的请求', '', 201);
            }

            $user_is_renzheng = Db::name("userhome")->where("id = {$uid}")->value("is_renzheng");
            if ($user_is_renzheng == 2) {
                $store_name = Db::name("userstore")->where("userhome_id = {$uid}")->value("store_name");
                $data['store_name'] = $store_name;//"德文华凯小店";
            } else {
                $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");//27
                if ($pid) {
                    $pid_user = Db::name("userhome")->where("id = {$pid}")->find();
                    $store_name = Db::name("userstore")->where("userhome_id = {$pid}")->value("store_name");
                    if ($pid_user['is_renzheng'] == 2) {
                        $data['store_name'] = $store_name;
                    } else {
                        $data['store_name'] = "乡米网";
                    }
                } else {
                    $data['store_name'] = "乡米网";
                }
            }
        } else {
            $data['store_name'] = "乡米网";
        }
        //banner图
        $banner = Db::name("banner")->order("weigh desc")->select();
        $sec = 0;
        foreach ($banner as $k => $v) {
            $sec = $v['sec'];
            $banner[$k]['image'] = path($v['image']);
            if ($v['jingang_id']) {
                $banner[$k]['jingang_name'] = DB::name('jingang')->where('id', $v['jingang_id'])->value('name');
            } else {
                $banner[$k]['jingang_name'] = '';
            }
        }
        //金刚
        $jingang = Db::name("jingang_anew")->where("status = 1")->field("*")->order("isIndex desc")->order("indexTime desc")->order("createtime desc")->select();
//        $jingang_goods = Db::name("jingang_newgoods a")
//            ->join('goods b', 'a.goods_id = b.id')
//            ->where("b.status = 1")
//            ->field("a.jingang_id,a.goods_id,b.name goods_name,b.price,b.old_price,b.image goods_image,b.sale_num")
////                        ->limit(($page-1)*10,10)
//            ->select();
        foreach ($jingang as $k => $v) {
            $jingang[$k]['beijing_image'] = path($v['beijing_image']);
            $jingang[$k]['image'] = path($v['image']);
            $jingang[$k]['title'] = $v['name'];
            $jingang[$k]['type'] = 1;
        }
//        foreach ($jingang as $k => $v) {
//            $jingang[$k]['image'] = path($v['image']);
//            foreach ($jingang_goods as $kk => $vv) {
//                if ($v['id'] == $vv['jingang_id']) {
//                    $jingang[$k]['goods'][] = $vv;
//                } else {
//                    $jingang[$k]['goods'] = array();
//                }
//            }
//        }
        //模块是否展示
        $is_show = Db::name("is_show")->field("id,name,title,etime,is_show")->select();
        //新人福利
        $newcomer_welfare = Db::name("newcomer_welfare a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.price,b.old_price,b.image,b.name,b.integral,b.province,b.city,vip_price")
            ->order('weigh desc')
//            ->limit(($page-1)*10,10)
            ->select();
        foreach ($newcomer_welfare as $k => $v) {
            $newcomer_welfare[$k]['image'] = path($v['image']);
        }
        //限时秒杀
        $seckill = Db::name("seckill a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.all_number,b.name,b.integral,a.number,a.price,b.old_price,a.image,b.province,b.city,vip_price")
            ->order('weigh desc')
//            ->limit(($page-1)*10,10)
            ->select();
        foreach ($seckill as $k => $v) {
            $seckill[$k]['image'] = path($v['image']);
        }
        //超值折扣
        $discount = Db::name("discount a")
            ->join("goods b", 'a.goods_id = b.id')
            ->where("b.status = 1")
            ->field("a.goods_id,a.is_show_id,a.price,b.old_price,a.image,b.name,b.integral,b.province,b.city,vip_price")
            ->order('weigh desc')
//            ->limit(($page-1)*10,10)
            ->select();
        foreach ($discount as $k => $v) {
            $discount[$k]['image'] = path($v['image']);
        }
        //合为一个数组
        foreach ($is_show as $k => $v) {
            $is_show[$k]['time'] = $v['etime'] - time();
            $is_show[$k]['discount'] = [];
            foreach ($newcomer_welfare as $k1 => $v1) {
                if ($v['id'] == $v1['is_show_id']) {
                    $is_show[$k]['newcomer_welfare'][] = $v1;
                }
            }
            foreach ($seckill as $k2 => $v2) {
                if ($v['id'] == $v2['is_show_id']) {
                    $is_show[$k]['seckill'][] = $v2;
                }
            }
            foreach ($discount as $k3 => $v3) {
                if ($v['id'] == $v3['is_show_id']) {
                    $is_show[$k]['discount'][] = $v3;
                }
            }
        }
        //为您推荐 7 天以内的搜索  没有 就从商品表随机抽取
//        if($uid){
//            $stime = strtotime('-7 days');
//            $search = Db::name("search")->where("uid = {$uid} and createtime >= {$stime}")->field("name,createtime")->select();
//            $names = array();
//            foreach($search as $v){
//                $names[] = $v['name'];
//            }
//
//            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
//            $goods_id = array();
//            if($pid){  //我的隐藏数据
//                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
//                foreach($useryincang as $v){
//                    $goods_id[] = $v['goods_id'];
//                }
//            }
//            $goods_ids = "";
//            if($goods_id){
//                $goods_ids = implode(',',$goods_id);
//            }
//
//            if($names){
//                $ids = array();
//                if($goods_ids){
//                    foreach($names as $k=>$v){
//                        $ids[] = Db::name("goods")->where("id not in ({$goods_ids}) and name like '%{$v}%' and status = 1 ")->field('id')->select();
//                    }
//                }else{
//                    foreach($names as $k=>$v){
//                        $ids[] = Db::name("goods")->where("name like '%{$v}%' and status = 1 ")->field('id')->select();
//                    }
//                }
//                $idss = array();
//                foreach($ids as $k=>$v){
//                    foreach($v as $kk=>$vv){
//                        $idss[] = $vv;
//                    }
//                }
//                $id = array();
//                foreach($idss as $k=>$v){
//                    $id[] = $v['id'];
//                }
//                shuffle($id);
//
//                $goods = array();
//                $goods2 = Db::name("goods");
//                if($id){
//                    foreach($id as $v){
//                        $goods[] = $goods2
//                            ->where("id = {$v}")
//                            ->field("id,name,image,price,old_price,sale_num")
////                            ->limit(($page-1)*10,10)
//                            ->find();
//                    }
//                }else{
//                    $goods = Db::name("goods")->where("id not in ({$goods_ids}) and status = 1")->field("id")->select();
//                    $id = array();
//                    foreach($goods as $v){
//                        $id[] = $v['id'];
//                    }
//                    shuffle($id);
//                    $goods = array();
//                    $goods2 = Db::name("goods");
//                    if($id){
//                        foreach($id as $v){
//                            $goods[] = $goods2
//                                ->where("id = {$v}")
//                                ->field("id,name,image,price,old_price,sale_num")
////                        ->limit(($page-1)*10,10)
//                                ->find();
//                        }
//                    }
//                }
//            }else{
//                if($goods_ids){
//                    $goods = Db::name("goods")->where("id not in ({$goods_ids}) and status = 1")->field("id")->select();
//                }else{
//                    $goods = Db::name("goods")->where("status = 1")->field("id")->select();
//                }
//                $id = array();
//                foreach($goods as $v){
//                    $id[] = $v['id'];
//                }
//                shuffle($id);
//                $goods = array();
//                $goods2 = Db::name("goods");
//                if($id){
//                    foreach($id as $v){
//                        $goods[] = $goods2
//                            ->where("id = {$v}")
//                            ->field("id,name,image,price,old_price,sale_num")
////                            ->limit(($page-1)*10,10)
//                            ->find();
//                    }
//                }
//            }
//        }else{
//            $goods = Db::name("goods")->where("status = 1")->field("id")->select();
//            $id = array();
//            foreach($goods as $v){
//                $id[] = $v['id'];
//            }
//            shuffle($id);
//            $goods = array();
//            $goods2 = Db::name("goods");
//            if($id){
//                foreach($id as $v){
//                    $goods[] = $goods2
//                        ->where("id = {$v}")
//                        ->field("id,name,image,price,old_price,sale_num")
////                        ->limit(($page-1)*10,10)
//                        ->find();
//                }
//            }
//        }
//        foreach($goods as $k=>$v){
//            $goods[$k]['image'] = path($v['image']);
//        }

        $data['lunbo']['sec'] = $sec;
        $data['lunbo']['banner'] = $banner;
        $data['jingang'] = $jingang;
        $data['activity'] = $is_show;
//        $data['tuijian'] = $goods;
        $this->success('成功', $data, 200);
    }

    /*
     * 搜索历史和大家都在搜
     */
    public function all_search()
    {
        $request = [
            'uid' => $this->request->param('uid'),//经纬度
        ];
        /* 验证不能为空的字段*/
        $uid = $request['uid'];
        //大家都在搜
        $all = Db::name("all_search")->field('name')->select();
        $all_arr = array();
        foreach ($all as $v) {
            $all_arr[] = $v['name'];
        }
        //搜索历史
        $my_arr = array();
        if ($uid) {

            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
            // if ($res == 0) {
            //     $this->error('账号异常','',206);
            // }
            // if ($res == 1) {
            //     $this->error('非法的请求','',201);
            // }

            $my = Db::name("search")->where("uid = {$uid}")->order('id', 'desc')->field('name')->group('name')->select();
            foreach ($my as $v) {
                $my_arr[] = $v['name'];
            }
        }

        $data['my'] = $my_arr;
        $data['all'] = $all_arr;
        $this->success('成功', $data, 200);
    }

    /*
     * 9.搜索结果
     */
    public function search_result()
    {
        $request = [
            'uid' => $this->request->param('uid'),//用户id
            'name' => $this->request->param('name'),//名称
            'type' => $this->request->param('type'),//类型:1价格从高到低  2价格从低到高   3销量从高到底
            'page' => $this->request->param('page'),
            'page_count' => $this->request->param('page_count') ?: 10,
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('name');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $name = $request['name'];
        $type = $request['type'];
        $page_count = $request['page_count'];

        //如果uid 有值 往搜索表加记录
        if ($uid) {
//            $res = yanzheng($uid,$this->request->param("token"),$this->request->param("jm_str"));
//            if ($res == 0) {
//                $this->error('账号异常','',206);
//            }
//            if ($res == 1) {
//                $this->error('非法的请求','',201);
//            }

            $da = array();
            $da['uid'] = $uid;
            $da['name'] = $name;
            $da['createtime'] = time();
            Db::name("search")->insert($da);

            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");

            $goods_id = array();
            if ($pid) {  //我的隐藏数据
                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
                foreach ($useryincang as $v) {
                    $goods_id[] = $v['goods_id'];
                }

            }

            $goods_ids = "";
            if ($goods_id) {
                $goods_ids = implode(',', $goods_id);
            }

            if ($goods_ids) {
                $where = " 1 and status = 1 and id not in ({$goods_ids})";
            } else {
                $where = " 1 and status = 1";
            }
        } else {
            $where = " 1 and status = 1";
        }

        if ($name) {
            $where .= " and name like '%{$name}%'";
        }
        $page = $request['page'];
        if (!$page) {
            $page = 1;
        }
        if ($type == 1) {
            $data = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                ->order("price desc")
                ->page($page, $page_count)
                ->select();
        } elseif ($type == 2) {
            $data = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                ->order("price asc")
                ->page($page, $page_count)
                ->select();
        } elseif ($type == 3) {
            $data = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                ->order("sale_num desc")
                ->page($page, $page_count)
                ->select();
        } else {
            $data = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                ->page($page, $page_count)
                ->select();
        }

        foreach ($data as $k => $v) {
            $data[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $data, 200);
    }

    /*
   * 猜你喜欢
   */
    public function userlike()
    {
        $request = [
            'uid' => $this->request->param('uid'),//用户id
            'page' => $this->request->param('page'),
        ];

        $uid = $request['uid'];
        $page = $request['page'];
        if (!$page) {
            $page = 1;
        }
        //如果uid 有值 往搜索表加记录
        $data = array();
        if ($uid) {
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
            if ($res == 0) {
                $this->error('账号异常', '', 206);
            }
            if ($res == 1) {
                $this->error('非法的请求', '', 201);
            }

            $one_class = Db::name("userlike")->where("userhome_id = {$uid}")->field("goods_one_type_id")->select();
            $one_class_arr = array();
            foreach ($one_class as $v) {
                $one_class_arr[] = $v['goods_one_type_id'];
            }
            $one_class_arrs = implode(',', $one_class_arr);

            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
            $goods_id = array();
            if ($pid) {  //我的隐藏数据
                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
                foreach ($useryincang as $v) {
                    $goods_id[] = $v['goods_id'];
                }
            }
            $goods_ids = "";
            if ($goods_id) {
                $goods_ids = implode(',', $goods_id);
            }
            if ($goods_ids) {
                $where = " 1 and status = 1 and is_del != 1 and id not in ({$goods_ids})";
            } else {
                $where = " 1 and status = 1 and is_del != 1";
            }

            if ($one_class_arrs) {
                $where .= " and goods_one_type_id in ($one_class_arrs)";
                $data = Db::name("goods")
                    ->where($where)
                    ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                    ->order("price desc")
                    ->limit(($page - 1) * 10, 10)
                    ->select();
            }
        } else {

            $one_class = Db::name("goods_one_type")->field("id")->select();
            $ids = array();
            foreach ($one_class as $v) {
                $ids[] = $v['id'];
            }
            $id2 = array_rand($ids, 3);
            $ids3 = array();
            foreach ($ids as $k => $v) {
                foreach ($id2 as $kk => $vv) {
                    if ($k == $vv) {
                        $ids3[] = $v;
                    }
                }
            }
            $ids3 = implode(',', $ids3);
            $where = " 1 and status = 1 and is_del != 1 ";
            if ($ids3) {
                $where .= " and goods_one_type_id in ($ids3)";
                $data = Db::name("goods")
                    ->where($where)
                    ->field("id,name,price,old_price,sale_num,image,integral,province,city,vip_price")
                    ->order("price desc")
                    ->limit(($page - 1) * 10, 10)
                    ->select();
            }
        }
        shuffle($data);
        foreach ($data as $k => $v) {
            $data[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $data, 200);
    }

    /*
     * 商品一级分类
     */
    public function goods_one_type()
    {
        $data = Db::name("goods_one_type")->where('is_yc_switch', 0)->order("weigh asc")->field("id,name")->select();
        $this->success('成功', $data, 200);
    }

    /*
     * 商品二级分类
     */
    public function goods_two_type()
    {
        $request = [
            'goods_one_type_id' => $this->request->param('goods_one_type_id'),//一级分类id
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('goods_one_type_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $goods_one_type_id = $request['goods_one_type_id'];
        $data = Db::name("goods_two_type")->where("goods_one_type_id = {$goods_one_type_id}")->where('is_yc_switch', 0)->field("id,name,image")->order("weigh asc")->select();
        foreach ($data as $k => $v) {
            $data[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $data, 200);
    }

    public function goods_goods_three_type()
    {
        $goods_two_type_id = $this->request->param('goods_two_type_id');
        if (!$goods_two_type_id) {
            $this->error("不能没有二级分类id", '', 201);
        }
        $goods_three_type = Db::name("goods_three_type")->where("goods_two_type_id = {$goods_two_type_id}")->field("id,name")->select();
        $this->success('成功', $goods_three_type, 200);
    }

    /*
   * 23分类—商品列表
   */
    public function goods_lst()
    {
        $request = [
            'goods_two_type_id' => $this->request->param('goods_two_type_id'),//二级分类id
            'goods_three_type_id' => $this->request->param('goods_three_type_id'),//三级分类id
            'type' => $this->request->param('type'),//类型:1价格从高到低  2价格从低到高   3销量从高到底
            'name' => $this->request->param('name'),//商品名称
            'page' => $this->request->param('page') ?: 1,
            'page_count' => $this->request->param('page_count') ?: 10,
            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('goods_two_type_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $name = $request['name'];
        $type = $request['type'];
        $goods_two_type_id = $request['goods_two_type_id'];
        $goods_three_type_id = $request['goods_three_type_id'];
        $where = " 1 and goods_two_type_id = {$goods_two_type_id} and status = 1";
        if ($request['goods_three_type_id']) {
            $where .= " and goods_three_type_id = {$goods_three_type_id}";
        }
        $goods_ids = "";
        if ($uid) {
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
            // if ($res == 0) {
            //     $this->error('账号异常','',206);
            // }
            // if ($res == 1) {
            //     $this->error('非法的请求','',201);
            // }
            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
            $goods_id = array();
            if ($pid) {  //我的隐藏数据
                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
                foreach ($useryincang as $v) {
                    $goods_id[] = $v['goods_id'];
                }
            }

            if ($goods_id) {
                $goods_ids = implode(',', $goods_id);
                $where .= " and id not in ({$goods_ids})";
            }

        }


        if ($name) {
            $where .= " and name like '%{$name}%'";
        }
        $page = $request['page'];
        $page_count = $request['page_count'];

        if (!$page) {
            $page = 1;
        }
        $goods = array();

        if ($type == 1) {
            $goods = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,goods_three_type_id,integral,province,city,vip_price")
                ->order("price desc")
                // ->limit(($page-1)*10,10)
                ->page($page, $page_count)
                ->select();

        } elseif ($type == 2) {
            $goods = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,goods_three_type_id,integral,province,city,vip_price")
                ->order("price asc")
                // ->limit(($page-1)*10,10)
                ->page($page, $page_count)
                ->select();
        } elseif ($type == 3) {
            $goods = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,goods_three_type_id,integral,province,city,vip_price")
                ->order("sale_num desc")
                // ->limit(($page-1)*10,10)
                ->page($page, $page_count)
                ->select();
        } else {
            $goods = Db::name("goods")
                ->where($where)
                ->field("id,name,price,old_price,sale_num,image,goods_three_type_id,integral,province,city,vip_price")
                // ->limit(($page-1)*10,10)
                ->page($page, $page_count)
                ->select();
        }
        foreach ($goods as $k => $v) {
            $goods[$k]['image'] = path($v['image']);
        }
        $data['goods'] = $goods;
        $this->success('成功', $data, 200);
    }


    /*
     * 11商品详情
     */
    public function goods_details()
    {
        $request = [
            'goods_id' => $this->request->param('goods_id'),//商品id
            'uid' => $this->request->param('uid'),//
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('goods_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
//        if($uid){
//            $res = yanzheng($uid,$this->request->param("token"),$this->request->param("jm_str"));
//            // if ($res == 0) {
//            //     $this->error('账号异常','',206);
//            // }
//            // if ($res == 1) {
//            //     $this->error('非法的请求','',201);
//            // }
//        }

        $goods_id = $request['goods_id'];
        $goods = Db::name("goods")
            ->where("id = {$goods_id}")
            ->field("id,name,price,old_price,sale_num,image,images,introduce,chandi,goods_one_type_id,brand,parameter,zhongliang,expressfee_id,is_baoyou,status,is_del,spec1,spec2,content,integral,province,city,vip_price")
            ->find();
        if ($uid) {
            $userLink = Db::name("user_link")->where("userId = $uid")->find();
            if (!$userLink) {
                $userLink = [];
                $userLink['userId'] = $uid;
                $userLink['goodsOneTypeIdList'] = $goods['goods_one_type_id'];
                Db::name("user_link")->insert($userLink);
            } else {
                if (isset($userLink['goodsOneTypeIdList']) && !empty($userLink['goodsOneTypeIdList'])) {
                    $goodsOneTypeIdList = explode(';', $userLink['goodsOneTypeIdList']);
                    if (count($goodsOneTypeIdList) >= 5) {
                        unset($goodsOneTypeIdList[4]);
                        $goodsOneTypeIdList = $goods['goods_one_type_id'] . ";" . implode(';', $goodsOneTypeIdList);
                    } else {
                        $goodsOneTypeIdList = $goods['goods_one_type_id'] . ";" . $userLink['goodsOneTypeIdList'];
                    }
                } else {
                    $goodsOneTypeIdList = $goods['goods_one_type_id'];
                }
                Db::name("user_link")->where("userId = $uid")->update(['goodsOneTypeIdList' => $goodsOneTypeIdList]);
            }
        }
        $goods['share_integral'] = 0;
        $goods['oneself_integral'] = 0;
        if ($goods['integral'] != 0) {
            $system = Db::name("system")->where('systemId', 1)->find();
            if ($system) {
                $shareRate = $system['shareRate'];
            } else {
                $shareRate = 150;
            }
            $shareUser = DB::name('userhome t1')
                ->join('level t2', 't1.levelId=t2.levelId')
                ->whereIn('t1.id', $uid)
                ->field('t1.id,t1.phone,t2.ratio,t2.weight,t2.levelId')
                ->find();
            $weight = 20;
            if ($shareUser) {
                $weight = $shareUser['weight'];
            }
            $share_integral = intval(($goods['integral'] * 100) * $shareRate * $weight) / 1000000;
            $goods['share_integral'] = $share_integral;
            $goods['oneself_integral'] = $goods['integral'];
        }
        $goods['content'] = content_url($goods['content']);
        $goods['image'] = path($goods['image']);
        $images = explode(',', $goods['images']);
        $image = array();
        foreach ($images as $kk => $vv) {
            $image[] = path($vv);
        }
        $goods['images'] = $image;
        //运费信息
        $yunfei = Db::name("expressfee")->where("id = {$goods['expressfee_id']}")->find();

        //属性
        $kucun = Db::name("goods_stock")->where("goods_id = {$goods_id}")->select();
        $goods_spec_one_id = array();
        $goods_spec_two_id = array();
        foreach ($kucun as $k => $v) {
            $goods_spec_one_id[] = $v['goods_spec_one_id'];
            $goods_spec_two_id[] = $v['goods_spec_two_id'];
        }
        $goods_spec_one_id = implode(',', $goods_spec_one_id);
        $goods_spec_two_id = implode(',', $goods_spec_two_id);
        $goods_spec_one = array();
        if ($goods_spec_one_id) {
            $goods_spec_one = Db::name("goods_spec_one")->where("id in ($goods_spec_one_id)")->field("id,name")->select();
            if ($goods['spec2'] == '') {
                $goods_stock = DB::name('goods_stock');
                foreach ($goods_spec_one as $key => $value) {
                    $goods_spec_one[$key]['num'] = $goods_stock->where('goods_spec_one_id', $value['id'])->value('num');
                }
            }
        }
        $goods_spec_two = array();
        if ($goods_spec_two_id) {
            $goods_spec_two = Db::name("goods_spec_two")->where("id in ($goods_spec_two_id)")->field("id,name")->select();
        }
        //用户是否收藏
        $is_shoucang = 0;
        if ($uid) {

            $usercollection = Db::name("usercollection")->where("userhome_id = {$uid} and goods_id = {$goods_id}")->find();
            if ($usercollection) {
                $is_shoucang = 1;
            }
        }
        //用户评价
        $pingjia = Db::name("orderdetail a")
            ->join('userhome b', 'a.userhome_id = b.id')
            ->where("a.goods_id = {$goods_id} and a.is_pingjia = 1")
            ->field("b.avatar,b.nickname,a.leave,a.evaluate,a.images,a.pjtime")
            ->select();

        foreach ($pingjia as $k => $v) {
            $pingjia[$k]['avatar'] = path($v['avatar']);
            $pingjia[$k]['pjtime'] = date('Y-m-d H:i', $v['pjtime']);
            $pj_images = explode(',', $v['images']);
            foreach ($pj_images as $kk => $vv) {
                $pingjia[$k]['images2'][] = path($vv);
            }
        }
        //浏览记录
        if ($uid) {

            $goods_one_type_id = Db::name("goods")->where("id = {$goods_id}")->value("goods_one_type_id");
            $de = array();
            $de['userhome_id'] = $uid;
            $de['goods_one_type_id'] = $goods_one_type_id;
            $de['createtime'] = time();
            Db::name("userlike")->insert($de);
        }

        $goods['expressfee_info'] = $yunfei;
        $data = array();
        $data['is_shoucang'] = $is_shoucang;
        $data['goods'] = $goods;
        $data['yunfei'] = $yunfei;
        $data['goods_spec_one'] = $goods_spec_one;
        $data['goods_spec_two'] = $goods_spec_two;
        $data['pingjia'] = $pingjia;
        $is_show = Db::name("is_show")->where('name',"限时秒杀")->find();
        if(!empty($is_show)){
            $data['etime'] = $is_show['etime'] - time();
        }
        $this->success('成功', $data, 200);
    }

    /*
     * 14商品 选择规格
     */
    public function select_specifications()
    {
        $request = [
            'goods_id' => $this->request->param('goods_id'),//
            'goods_spec_one_id' => $this->request->param('goods_spec_one_id'),//
            'goods_spec_two_id' => $this->request->param('goods_spec_two_id'),//
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('goods_id', 'goods_spec_one_id', 'goods_spec_two_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $goods_id = $request['goods_id'];
        $goods_spec_one_id = $request['goods_spec_one_id'];
        $goods_spec_two_id = $request['goods_spec_two_id'];
        $data = Db::name("goods_stock")->where("goods_id = {$goods_id} and goods_spec_one_id = {$goods_spec_one_id} and goods_spec_two_id = {$goods_spec_two_id}")->find();

        $data['image'] = path($data['image']);
        $this->success('成功', $data, 200);
    }

    /*
    * 47个人中心—我的隐藏
    */
    public function my_yincang()
    {
        $request = [
            'uid' => $this->request->param('uid'),//
            'page' => $this->request->param('page'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('uid');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
        if ($res == 0) {
            $this->error('账号异常', '', 206);
        }
        if ($res == 1) {
            $this->error('非法的请求', '', 201);
        }


        $page = $request['page'];
        if (!$page) {
            $page = 1;
        }
        $useryincang = Db::name("useryincang")->where("userhome_id = {$uid}")->field("goods_id")->select();
        $goods_id = array();
        foreach ($useryincang as $v) {
            $goods_id[] = $v['goods_id'];
        }
        $goods_id = implode(',', $goods_id);
        $data = array();
        if ($goods_id) {
            $data = Db::name("goods")
                ->where("id in ($goods_id)")
                ->field("id,name,price,sale_num,image")
                ->limit(($page - 1) * 10, 10)
                ->select();
            foreach ($data as $k => $v) {
                $data[$k]['image'] = path($v['image']);
            }
        }
        $this->success('成功', $data, 200);
    }

    /*
     * 48个人中心—我的收藏
     */
    public function my_shoucang()
    {
        $request = [
            'uid' => $this->request->param('uid'),//
            'page' => $this->request->param('page'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('uid');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];

        $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
        if ($res == 0) {
            $this->error('账号异常', '', 206);
        }
        if ($res == 1) {
            $this->error('非法的请求', '', 201);
        }

        $page = $request['page'];
        if (!$page) {
            $page = 1;
        }
        $useryincang = Db::name("usercollection t1")
            ->join("goods t2", "t1.goods_id = t2.id")
            ->where("t1.userhome_id", $uid)
            ->order('t1.id', 'desc')
            ->field("t2.id,t2.name,t2.price,t2.sale_num,t2.image")
            ->limit(($page - 1) * 10, 10)
            ->select();
        foreach ($useryincang as $k => $v) {
            $useryincang[$k]['image'] = path($v['image']);
        }
        $this->success('成功', $useryincang, 200);
    }

    /*
     * 规格判断
     */
    public function is_specifications()
    {
        $request = [
            'goods_id' => $this->request->param('goods_id'),//
            'goods_spec_one_id' => $this->request->param('goods_spec_one_id'),//
            'type' => $this->request->param('type') ?: 0,//0正常价  1新人价 2秒杀价  3折扣价
            'uid' => $this->request->param('uid')
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('goods_id', 'goods_spec_one_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $goods_id = $request['goods_id'];
        $type = $request['type'];
        $goods_spec_one_id = $request['goods_spec_one_id'];
        $data = array();
        if ($type == 0) {
            $data = Db::name("goods_stock a")
                ->join('goods_spec_one c', 'a.goods_spec_one_id = c.id')
                ->where("a.goods_id = {$goods_id} and a.goods_spec_one_id = {$goods_spec_one_id}")
                ->field("a.id,c.name goods_spec_one_name,a.goods_id,a.goods_spec_one_id,a.goods_spec_two_id,a.num,a.price,a.image,a.use_integral,vip_price")
                ->select();
        } elseif ($type == 1) {
            $data = Db::name("goods_stock a")
                ->join('goods_spec_one c', 'a.goods_spec_one_id = c.id')
                ->where("a.goods_id = {$goods_id} and a.goods_spec_one_id = {$goods_spec_one_id}")
                ->field("a.id,c.name goods_spec_one_name,a.goods_id,a.goods_spec_one_id,a.goods_spec_two_id,a.num,a.new_price price,a.image,a.use_integral,vip_price")
                ->select();
        } elseif ($type == 2) {
            $data = Db::name("goods_stock a")
                ->join('goods_spec_one c', 'a.goods_spec_one_id = c.id')
                ->where("a.goods_id = {$goods_id} and a.goods_spec_one_id = {$goods_spec_one_id}")
                ->field("a.id,c.name goods_spec_one_name,a.goods_id,a.goods_spec_one_id,a.goods_spec_two_id,a.num,a.now_price price,a.image,a.use_integral,vip_price")
                ->select();
        } elseif ($type == 3) {
            $data = Db::name("goods_stock a")
                ->join('goods_spec_one c', 'a.goods_spec_one_id = c.id')
                ->where("a.goods_id = {$goods_id} and a.goods_spec_one_id = {$goods_spec_one_id}")
                ->field("a.id,c.name goods_spec_one_name,a.goods_id,a.goods_spec_one_id,a.goods_spec_two_id,a.num,a.zhekou_price price,a.image,a.use_integral,vip_price")
                ->select();
        }

        $goods_spec_two_id = array();
        foreach ($data as $k => $v) {
            $goods_spec_two_id[] = $v['goods_spec_two_id'];
            $data[$k]['image'] = path($v['image']);
        }
        $goods_spec_two_id = implode(',', $goods_spec_two_id);
        $goods_spec_two = array();
        if ($goods_spec_two_id) {
            $goods_spec_two = Db::name("goods_spec_two")->where("id in ($goods_spec_two_id)")->field("id,name")->select();
        }
        if ($goods_spec_two) {
            foreach ($data as $k => $v) {
                foreach ($goods_spec_two as $kk => $vv) {
                    if ($v['goods_spec_two_id'] == $vv['id']) {
                        $data[$k]['goods_spec_two_name'] = $vv['name'];
                    }
                }
            }
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['goods_spec_two_name'] = "";
            }
        }
        unset($k);
        unset($v);
        foreach ($data as $k => $v) {
            $system = Db::name("system")->where('systemId', 1)->find();
            if ($system) {
                $shareRate = $system['shareRate'];
            } else {
                $shareRate = 150;
            }
            $shareUser = DB::name('userhome t1')
                ->join('level t2', 't1.levelId=t2.levelId')
                ->where('t1.id', $uid)
                ->field('t1.id,t1.phone,t2.ratio,t2.weight,t2.levelId')
                ->find();
            $weight = 20;
            if ($shareUser) {
                $weight = $shareUser['weight'];
            }
            $share_integral = intval(($v['use_integral'] * 100) * $shareRate * $weight) / 1000000;
            $data[$k]['share_integral'] = $share_integral;
            $data[$k]['oneself_integral'] = $v['use_integral'];
            $data[$k]['integral'] = $v['use_integral'];
        }
        $this->success('成功', $data, 200);
    }

    /*
     * 删除搜索记录
     */
    public function del_jilu()
    {
        $request = [
            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('uid');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
        if ($res == 0) {
            $this->error('账号异常', '', 206);
        }
        if ($res == 1) {
            $this->error('非法的请求', '', 201);
        }
        Db::name("search")->where("uid = {$uid}")->delete();
        $this->success('成功', "", 200);
    }

    /*
     * 金刚模板
     */
    public function jingang_moban()
    {
        $request = [
            'jingang_id' => $this->request->param('jingang_id'),
            'uid' => $this->request->param('uid'),
            'page' => $this->request->param('page'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('jingang_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $goods_ids3 = "";
//        if($uid){
//            $res = yanzheng($uid,$this->request->param("token"),$this->request->param("jm_str"));
//            if ($res == 0) {
//                $this->error('账号异常','',206);
//            }
//            if ($res == 1) {
//                $this->error('非法的请求','',201);
//            }

//            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
//            $goods_id = array();
//            if($pid){  //我的隐藏数据
//                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
//                foreach($useryincang as $v){
//                    $goods_id[] = $v['goods_id'];
//                }
//            }
//
//            if($goods_id){
//                $goods_ids3 = implode(',',$goods_id);
//            }
//        }
        $jingang_id = $request['jingang_id'];
        $page = $request['page'];
        if (empty($page)) {
            $page = 1;
        }
        $data = Db::name("jingang")->where("id = {$jingang_id}")->find();

        $data['image'] = path($data['image']);
        $data['beijing_image'] = path($data['beijing_image']);
        $images = explode(',', $data['images']);
        $imagess = array();
        foreach ($images as $v) {
            $imagess[] = path($v);
        }
        $data['lunbotu'] = $imagess;
        //金刚分类 商品
        $jingang_class = Db::name("jingang_class")->where("jingang_id = {$jingang_id}")->order('weigh desc')->select();
        $jingang_newgoods = Db::name("jingang_newgoods")->select();
        $jingang_othergoods = Db::name("jingang_othergoods")->select();

        $goods_id = array();
        $goods_id2 = array();

        foreach ($jingang_class as $k => $v) {
            $jingang_class[$k]['image'] = path($v['image']);
            foreach ($jingang_newgoods as $kk => $vv) {
                if ($v['id'] == $vv['jingang_class_id']) {
                    $goods_id[] = $vv['goods_id'];
                }
            }
            foreach ($jingang_othergoods as $k2 => $v2) {
                if ($v['id'] == $v2['jingang_class_id']) {
                    $goods_id2[] = $v2['goods_id'];
                }
            }
        }

//        $goods_ids = implode(',',$goods_id);
//        $goods_ids2 = implode(',',$goods_id2);
//        $newgoods = array();


//        if($goods_ids){
//            if($goods_ids3){
//                $newgoods = Db::name("goods a")
//                    ->join('jingang_newgoods b','a.id = b.goods_id')
//                    ->where("a.id not in ($goods_ids3) and a.id in ({$goods_ids})")
//                    ->field("a.id,a.name,a.price,a.introduce,a.sale_num,a.image,b.jingang_class_id")
//                    ->limit(($page-1)*10,10)
//                    ->select();
//            }else{
//                $newgoods = Db::name("goods a")
//                    ->join('jingang_newgoods b','a.id = b.goods_id')
//                    ->where("a.id in ({$goods_ids})")
//                    ->field("a.id,a.name,a.price,a.introduce,a.sale_num,a.image,b.jingang_class_id")
//                    ->limit(($page-1)*10,10)
//                    ->select();
//            }
//
//            foreach($newgoods as $k=>$v){
//                $newgoods[$k]['image'] = path($v['image']);
////                foreach($jingang_newgoods as $kk=>$vv){
////                    if($v['id']==$vv['goods_id']){
////                        $newgoods[$k]['jingang_class_id'] = $vv['jingang_class_id'];
////                    }
////                }
//            }
//        }
//        $othergoods = array();
//        if($goods_ids2){
//            if($goods_ids3){
//                $othergoods = Db::name("goods")
//                    ->where("id not in ($goods_ids3) and id in ({$goods_ids2})")
//                    ->field("id,name,price,introduce,sale_num,image")
//                    ->limit(($page-1)*10,10)
//                    ->select();
//            }else{
//                $othergoods = Db::name("goods")
//                    ->where("id in ({$goods_ids2})")
//                    ->field("id,name,price,introduce,sale_num,image")
//                    ->limit(($page-1)*10,10)
//                    ->select();
//            }

//
//            foreach($othergoods as $k=>$v){
//                $othergoods[$k]['image'] = path($v['image']);
//                foreach($jingang_othergoods as $kk=>$vv){
//                    if($v['id']==$vv['goods_id']){
//                        $othergoods[$k]['jingang_class_id'] = $vv['jingang_class_id'];
//                    }
//                }
//            }
//        }
//var_dump($newgoods);die;
//        foreach($jingang_class as $k=>$v){
//            foreach($newgoods as $k1=>$v1){
//                 if($v['id']==$v1['jingang_class_id']){
//                     $jingang_class[$k]['newgoods'][] = $v1;
//                 }
//            }
//            foreach($othergoods as $k2=>$v2){
//                if($v['id']==$v2['jingang_class_id']){
//                    $jingang_class[$k]['othergoods'][] = $v2;
//                }
//            }
//        }

        $data['jingang_class'] = $jingang_class;
        $this->success('成功', $data, 200);
    }


    //21活动页
    public function get_jingang_goods()
    {
        $request = [
            'jingang_class_id' => $this->request->param('jingang_class_id'),
            'page' => $this->request->param('page') ?: 1,
            'page_count' => $this->request->param('page_count') ?: 10,

            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('jingang_class_id');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $jingang_class_id = $request['jingang_class_id'];
        $uid = $request['uid'];
        $goods_ids3 = "";
        if ($uid) {
            $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
//            if ($res == 0) {
//                $this->error('账号异常','',206);
//            }
//            if ($res == 1) {
//                $this->error('非法的请求','',201);
//            }

            $pid = Db::name("userhome")->where("id = {$uid}")->value("pid");
            $goods_id = array();
            if ($pid) {  //我的隐藏数据
                $useryincang = Db::name("useryincang")->where("userhome_id = {$pid}")->field('goods_id')->select();
                foreach ($useryincang as $v) {
                    $goods_id[] = $v['goods_id'];
                }
            }

            if ($goods_id) {
                $goods_ids3 = implode(',', $goods_id);
            }

        }

        $page = $request['page'];
        $page_count = $request['page_count'];
        if (empty($page)) {
            $page = 1;
        }
        //金刚分类 商品
        $jingang_class = Db::name("jingang_class")->where("id = {$jingang_class_id}")->find();
        $jingang_newgoods = Db::name("jingang_newgoods")->where("jingang_class_id = {$jingang_class_id}")->order('weigh desc')->select();
        $jingang_othergoods = Db::name("jingang_othergoods")->where("jingang_class_id = {$jingang_class_id}")->select();

        $goods_id = array();
        $goods_id2 = array();

        $jingang_class['image'] = path($jingang_class['image']);
        foreach ($jingang_newgoods as $kk => $vv) {
            $goods_id[] = $vv['goods_id'];
        }

        foreach ($jingang_othergoods as $k2 => $v2) {
            $goods_id2[] = $v2['goods_id'];
        }

        $goods_ids = implode(',', $goods_id);
        $goods_ids2 = implode(',', $goods_id2);
        $newgoods = array();
        // dump($jingang_newgoods);die;
        if ($goods_ids) {
            if ($goods_ids3) {
                $newgoods = Db::name("goods")
                    ->where("id not in ({$goods_ids3}) and id in ({$goods_ids}) and status = 1")
                    ->field("id,name,price,introduce,sale_num,image,integral,province,city,vip_price")
                    ->page($page, $page_count)
                    ->select();
            } else {
                $newgoods = Db::name("goods")
                    ->where("id in ({$goods_ids}) and status = 1")
                    ->field("id,name,price,introduce,sale_num,image,integral,province,city,vip_price")
                    ->page($page, $page_count)
                    ->select();
            }

            foreach ($newgoods as $k => $v) {
                $newgoods[$k]['image'] = path($v['image']);
                foreach ($jingang_newgoods as $kk => $vv) {
                    if ($v['id'] == $vv['goods_id']) {
                        $newgoods[$k]['jingang_class_id'] = $vv['jingang_class_id'];
                    }
                }
            }
            foreach ($jingang_newgoods as $k11 => $v11) {
                foreach ($newgoods as $k22 => $v22) {
                    if ($v11['goods_id'] == $v22['id']) {
                        $newgoods[$k22]['sort'] = $v11['weigh'];
                    }
                }
            }
            $sorts = array_column($newgoods, 'sort');
            array_multisort($sorts, SORT_DESC, $newgoods);

        }
        $othergoods = array();
        if ($goods_ids2) {

            if ($goods_ids3) {
                $othergoods = Db::name("goods")
                    ->where("id not in ({$goods_ids3}) and id in ({$goods_ids2}) and status = 1")
                    ->field("id,name,price,introduce,sale_num,image,integral,province,city,vip_price")
                    ->page($page, $page_count)
                    ->select();
            } else {
                $othergoods = Db::name("goods")
                    ->where("id in ({$goods_ids2}) and status = 1")
                    ->field("id,name,price,introduce,sale_num,image,integral,province,city,vip_price")
                    ->page($page, $page_count)
                    ->select();
            }


            foreach ($othergoods as $k => $v) {
                $othergoods[$k]['image'] = path($v['image']);
                foreach ($jingang_othergoods as $kk => $vv) {
                    if ($v['id'] == $vv['goods_id']) {
                        $othergoods[$k]['jingang_class_id'] = $vv['jingang_class_id'];
                    }
                }
            }
            //
            foreach ($jingang_othergoods as $k11 => $v11) {
                foreach ($othergoods as $k22 => $v22) {
                    if ($v11['goods_id'] == $v22['id']) {
                        $othergoods[$k22]['sort'] = $v11['weigh'];
                    }
                }
            }
            $sorts = array_column($othergoods, 'sort');
            array_multisort($sorts, SORT_DESC, $othergoods);
            //
        }
        $data = array();
        $data['newgoods'] = $newgoods;
        $data['othergoods'] = $othergoods;
        $this->success('成功', $data, 200);
    }

    //是否是新人
    public function is_new()
    {
        $request = [
            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('uid');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
        if ($res == 0) {
            $this->error('账号异常', '', 206);
        }
        if ($res == 1) {
            $this->error('非法的请求', '', 201);
        }
        $order = Db::name("order")->where("userhome_id = {$uid} and status > 1")->find();
        $is_new = 1;
        if ($order) {
            $is_new = 2;
        }
        $this->success('成功', $is_new, 200);
    }

    public function is_last()
    {
        $request = [
            'uid' => $this->request->param('uid'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('uid');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $uid = $request['uid'];
        $res = yanzheng($uid, $this->request->param("token"), $this->request->param("jm_str"));
        if ($res == 0) {
            $this->error('账号异常', '', 206);
        }
        if ($res == 1) {
            $this->error('非法的请求', '', 201);
        }
        $user = Db::name("userhome")->where('id', $uid)->find();
        $userRelation = Db::name("user_relation")->where('userId', $user['id'])->find();
        $is_last = 0;
        if (isset($userRelation['inviteUserId']) && !empty($userRelation['inviteUserId'])) {
            $is_last = 1;
        }
        $this->success('成功', $is_last, 200);
    }


    //版本提审控制
    public function banben()
    {
        $request = [
            'banben_number' => $this->request->param('banben_number'),
        ];
        /* 验证不能为空的字段*/
        $EmptyData = array('banben_number');
        foreach ($EmptyData as $oeVo) {
            $retInfo = EmptyValidate($oeVo, $request);
            if ($retInfo) {
                $this->error($retInfo, '', 201);
            }
        }
        $banben_number = $request['banben_number'];
        $data = Db::name("banben")->where("banben = '{$banben_number}'")->value('status');
        $this->success('成功', $data, 200);
    }
}
